﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities.IRepository;
using DAL.Admin;
using Entities;
using BLL.BLL;

namespace WindowsForms.Admin
{
    public partial class FormThemBoPhan : Form
    {
        public static BoPhan bophan;
        int quyen;
        public IBoPhanRepository bp = new BLTBoPhanRepository();
        public FormThemBoPhan()
        {
            InitializeComponent();
        }

        private void btluu_Click(object sender, EventArgs e)
        {
            BoPhan bophan = new BoPhan();
            if (!String.IsNullOrWhiteSpace(tbmabophan.Text))
            {
                bophan.MaBoPhan = tbmabophan.Text;
                bophan.TenBoPhan = tbtenbophan.Text;
                bophan.PhanQuyen = quyen;
                if (!QuanLiBoPhan.KiemTra(bophan.MaBoPhan, bp.LayTatCa()))
                {
                    bp.ThemBoPhan(bophan);
                    MessageBox.Show("Sccessful");
                }
                else
                {
                    DialogResult rs = MessageBox.Show("Đã có trong CSDL bạn muốn cập nhật không ?","Thông Báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                    if (rs == DialogResult.Yes)
                    {
                        bp.CapNhatBoPhan(bophan);
                        MessageBox.Show("Sccessful");
                    }
                }
            }
            else MessageBox.Show("Nhập Lại Mã BP");
        }

        private void cbbquyen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbquyen.SelectedIndex > 0)
            {
                quyen =Convert.ToInt32( cbbquyen.SelectedItem.ToString());
 
            }
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void GanGiaTriChoTestBoPhan(BoPhan bophan)
        {
            tbmabophan.Text = bophan.MaBoPhan;
            tbmabophan.ReadOnly = true;
            tbtenbophan.Text = bophan.TenBoPhan;
            for (int i = 1; i < cbbquyen.Items.Count; i++)
                if (cbbquyen.Items[i].ToString() == bophan.PhanQuyen + "")
                    cbbquyen.SelectedIndex = i;
            
        }

        private void FormThemBoPhan_Load(object sender, EventArgs e)
        {
            GanGiaTriChoTestBoPhan(bophan);
        }
 
       
    }
}
